﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Data.SqlClient;
using pcsse.App_Code.entity;

namespace pcsse.BBS
{

    public partial class ShowPost : System.Web.UI.Page
    {
        //protected string Branch_Name="";
        string subject_name;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["ID"] != null && Request["ID"] != null)
            {
                SiteMap.SiteMapResolve += new SiteMapResolveEventHandler(this.ResolveForumPaths);
                if (Convert.ToInt32(Session["Priority"]) >= 3)
                {
                    ListView1.Visible = false;
                    ListView2.Visible = true;
                }
                SqlCommand cmd2 = new SqlCommand();
                cmd2.CommandText = "select SUBJECT from T_SUBJECT where ID=@ID";
                cmd2.Parameters.Add("@ID", Request["ID"].ToString());
                DataTable dt2 = db_base.getDBhandle().GetDataTableByCmd(cmd2);
                if (dt2.Rows.Count == 1)
                {
                    subject_name = dt2.Rows[0][0].ToString();
                    Label_sname.Text = subject_name;
                }
                else
                    Response.Redirect("..\\");
                
            }
            else
                Response.Redirect("..\\");
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Request["ID"] != null)
            {
                DateTime time = DateTime.Now;
                db_base db = db_base.getDBhandle();
                person_info pi = new person_info();
                string student_id = pi.GetNumberByID(Convert.ToInt32(Session["ID"]));
                string sqlstr1 = "INSERT INTO T_POST(SUBJECT_ID,TITLE,TEXT,STUDENT_ID,TIME,IP) VALUES(@SUBJECT_ID,@TITLE,@TEXT,@STUDENT_ID,@TIME,@IP)";
                SqlCommand cmd1 = new SqlCommand();
                cmd1.CommandText = sqlstr1;
                cmd1.Parameters.Add("@SUBJECT_ID", Request["ID"].ToString());
                cmd1.Parameters.Add("@TITLE", TextBox1.Text);
                cmd1.Parameters.Add("@TEXT",textcontent.Value);
                cmd1.Parameters.Add("@STUDENT_ID",student_id);
                cmd1.Parameters.Add("@TIME", time.ToString());
                cmd1.Parameters.Add("@IP", Request.UserHostAddress);
                db.ExecuteCmd(cmd1);
                SqlCommand cmd2 = new SqlCommand();
                cmd2.CommandText = "SELECT ID FROM T_POST WHERE STUDENT_ID=@STUDENT_ID AND TIME =@TIME";
                cmd2.Parameters.Add("@STUDENT_ID",student_id);
                cmd2.Parameters.Add("@TIME", time.ToString());
                DataTable dt = db.GetDataTableByCmd(cmd2);
                string sqlstr2 = "INSERT INTO T_REPLY(TITLE_ID,TEXT,STUDENT_ID,TIME,IP) VALUES(@TITLE_ID,@TEXT,@STUDENT_ID,@TIME,@IP)";
                SqlCommand cmd3 = new SqlCommand();
                cmd3.CommandText = sqlstr2;
                cmd3.Parameters.Add("@TITLE_ID", dt.Rows[0][0].ToString());
                cmd3.Parameters.Add("@TEXT", textcontent.Value);
                cmd3.Parameters.Add("@STUDENT_ID", student_id);
                cmd3.Parameters.Add("@TIME", time.ToString());
                cmd3.Parameters.Add("@IP", Request.UserHostAddress);
                db.ExecuteCmd(cmd3);
                string url = "Post.aspx?ID=";
                url += dt.Rows[0][0].ToString();
                Response.Redirect(url);
            }
        }

        private SiteMapNode ResolveForumPaths(Object sender, SiteMapResolveEventArgs e)
        {
            SiteMap.SiteMapResolve -= new SiteMapResolveEventHandler(this.ResolveForumPaths);
            SiteMapNode currentNode = SiteMap.CurrentNode.Clone(true);
            SiteMapNode tempNode = currentNode;
            string curPageName = e.Context.Request.AppRelativeCurrentExecutionFilePath.Substring(2).ToUpper();
            switch (curPageName)
            {
                case "BBS/POST.ASPX":
                    break;
                case "BBS/SHOWPOST.ASPX":
                    tempNode.Title = subject_name;
                    break;
            }
            return currentNode;
        }
    }
}
